package com.newenergy.wisehome.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import com.newenergy.wisehome.R;
import com.newenergy.wisehome.config.AppManager;
import com.newenergy.wisehome.data.bean.DeviceCategory;
import com.newenergy.wisehome.data.ormlite.dao.DeviceCategoryDao;

import butterknife.BindView;
import butterknife.OnClick;
import me.yokeyword.fragmentation.ISupportFragment;

/**
 * Created by kyx on 2018/8/21.
 * 风扇灯操作页面--7203
 */

public class DataFanLampFragment extends BaseFragment implements SeekBar.OnSeekBarChangeListener{


    private static String ARG_FANLAMP = "arg_fanlamp";

    @BindView(R.id.btn_back)
    Button mBtnBack;
    @BindView(R.id.tv_back)
    TextView mTvBack;
    @BindView(R.id.tv_page)
    TextView mTvPage;
    @BindView(R.id.btn_lamp_switch)
    Button mBtnLampSwitch;
    @BindView(R.id.btn_fan_switch)
    Button mBtnFanSwitch;
    @BindView(R.id.btn_time1)
    Button mBtnTime1;
    @BindView(R.id.btn_time2)
    Button mBtnTime2;
    @BindView(R.id.btn_time4)
    Button mBtnTime4;
    @BindView(R.id.btn_slow)
    Button mBtnSlow;
    @BindView(R.id.btn_middle)
    Button mBtnMiddle;
    @BindView(R.id.btn_fast)
    Button mBtnFast;
    @BindView(R.id.btn_subsection)
    Button mBtnSubsection;

    @BindView(R.id.seekbar_brg)
    SeekBar mSeekbarBrg;
    @BindView(R.id.seekbar_cw)
    SeekBar mSeekbarCW;


    protected DeviceCategory mDeviceCategory;
    private long mCurrTime = 0;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_fanlamp_data;
    }


    public static ISupportFragment newInstance(DeviceCategory mainItem) {
        Bundle args = new Bundle();
        args.putParcelable(ARG_FANLAMP, mainItem);
        DataFanLampFragment fragment = new DataFanLampFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void init(View rootView) {
        mDeviceCategory = getArguments().getParcelable(ARG_FANLAMP);
        Log.e("LFX", mDeviceCategory.toString());
        if (mDeviceCategory != null) {
            mTvPage.setText(mDeviceCategory.getName());
        }

        mSeekbarBrg.setProgress(mDeviceCategory.getLd());
        mSeekbarCW.setProgress(mDeviceCategory.getSw());
        mSeekbarBrg.setOnSeekBarChangeListener(this);
        mSeekbarCW.setOnSeekBarChangeListener(this);
    }


    @Override
    public void onResume() {
        super.onResume();
        if (mDeviceCategory != null) {
            AppManager.getInstance(getContext()).mSelectDeviceCategory = mDeviceCategory;
        }
    }

    @OnClick({R.id.btn_back, R.id.tv_back, R.id.btn_lamp_switch, R.id.btn_fan_switch, R.id.btn_time1, R.id.btn_time2, R.id.btn_time4, R.id.btn_slow, R.id.btn_middle, R.id.btn_fast,
            R.id.btn_subsection})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                doBack();

                break;
            case R.id.tv_back:
                doBack();

                break;
            case R.id.btn_subsection://分段
                AppManager.getInstance(mainActivity).commandAdvertise("01", mDeviceCategory.getFamilyId(), mDeviceCategory.getGroupId(), "0D000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

                break;
            case R.id.btn_lamp_switch://灯开关
                AppManager.getInstance(mainActivity).commandAdvertise("01", mDeviceCategory.getFamilyId(), mDeviceCategory.getGroupId(), "01000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

                break;
            case R.id.btn_fan_switch://风扇关
                AppManager.getInstance(mainActivity).commandAdvertise("01", mDeviceCategory.getFamilyId(), mDeviceCategory.getGroupId(), "02000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

                break;
            case R.id.btn_auxiliary://辅光
                AppManager.getInstance(mainActivity).commandAdvertise("01", mDeviceCategory.getFamilyId(), mDeviceCategory.getGroupId(), "0E000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

                break;
            case R.id.btn_time1://定时一小时
                AppManager.getInstance(mainActivity).commandAdvertise("01", mDeviceCategory.getFamilyId(), mDeviceCategory.getGroupId(), "03000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

                break;
            case R.id.btn_time2://定时两小时
                AppManager.getInstance(mainActivity).commandAdvertise("01", mDeviceCategory.getFamilyId(), mDeviceCategory.getGroupId(), "04000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

                break;
            case R.id.btn_time4://定时四小时
                AppManager.getInstance(mainActivity).commandAdvertise("01", mDeviceCategory.getFamilyId(), mDeviceCategory.getGroupId(), "05000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

                break;
            case R.id.btn_slow://慢
                AppManager.getInstance(mainActivity).commandAdvertise("01", mDeviceCategory.getFamilyId(), mDeviceCategory.getGroupId(), "06000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

                break;
            case R.id.btn_middle://中
                AppManager.getInstance(mainActivity).commandAdvertise("01", mDeviceCategory.getFamilyId(), mDeviceCategory.getGroupId(), "07000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

                break;
            case R.id.btn_fast://快
                AppManager.getInstance(mainActivity).commandAdvertise("01", mDeviceCategory.getFamilyId(), mDeviceCategory.getGroupId(), "08000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

                break;
        }
    }


    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        if (!checkAdvertisIntval()){
            return;
        }

        switch (seekBar.getId()) {
            case R.id.seekbar_brg:
                sendPWMValue(1, progress);

                break;

            case R.id.seekbar_cw:
                sendPWMValue(2, progress);
                 break;

        }

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {


    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        mCurrTime = 0;
        switch (seekBar.getId()) {
            case R.id.seekbar_brg:
                sendPWMValue(1, seekBar.getProgress());

                break;

            case R.id.seekbar_cw:
                sendPWMValue(2, seekBar.getProgress());
                break;

        }
    }




    private void sendPWMValue(int tag,int progress) {
        int ld = mDeviceCategory.getLd();
        int sw = mDeviceCategory.getSw();
        DeviceCategoryDao deviceCategoryDao = new DeviceCategoryDao(getContext());

        if (tag == 1) {//调亮度
            ld = progress+10;
            mDeviceCategory.setLd(ld);
        } else if (tag == 2) {//调色温
            sw = progress;
            mDeviceCategory.setSw(sw);
        }
        deviceCategoryDao.updateDeviceCategory(mDeviceCategory);

        int pwm1 = 255;
        int pwm2 = 255;

        if (sw <= 127) {
            pwm1 = sw * 2;
        } else {
            pwm2 = (255 - sw) * 2;
        }

        pwm1 = ld * pwm1 / 255;
        pwm2 = ld * pwm2 / 255;

        pwm1 = pwm1 > 255 ? 255 : pwm1;
        pwm2 = pwm2 > 255 ? 255 : pwm2;

        String data1 = numToHex(pwm1) + numToHex(pwm2)+"0000";
        AppManager.getInstance(mainActivity).commandAdvertise("02", mDeviceCategory.getFamilyId(), mDeviceCategory.getGroupId(), data1, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mDeviceCategory = null;

    }


    private boolean checkAdvertisIntval() {
        long currentTimeMillis = System.currentTimeMillis();
        long   intval  = currentTimeMillis - mCurrTime;
        if (intval> 120){
            mCurrTime = currentTimeMillis;
            return true;
        }
        return false;
    }
}
